package com.huwaiwai.fork_join.demo02;

import java.util.concurrent.*;

/**
 * Created by huweiwei on 2017/4/18.
 */
public class MyCache implements IMyCache{

    private final static ConcurrentHashMap<String, Object> cache = new ConcurrentHashMap<>();

    @Override
    public Object get(String key, Callable callable) throws ExecutionException, InterruptedException {
        System.out.println("进入get" + Thread.currentThread().getName());
        Object obj = cache.get(key);
        if (obj != null) {
            return obj;
        }
        FutureTask futureTask = new FutureTask(callable);
        new Thread(futureTask).start();
        System.out.println(futureTask.isDone());
        System.out.println(futureTask.get());
        System.out.println(futureTask.isDone());
        cache.putIfAbsent(key, futureTask.get());
        return cache.get(key);
    }
}
